package middleware

import (
	"net/http"
	"robot/cache"
)

func CheckAuth(next func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		c, err := r.Cookie("session")
		if err != nil || cache.Session.Get(c.Value) == "" {
			http.Redirect(w, r, r.URL.Host+"/login", http.StatusFound)
		}

		next(w, r)
	}
}
